import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


public class HelpMenu extends ScreenManager{

	private static final long serialVersionUID = 1L;
	private String userName= "";
	private JLabel playGameLabel, settingsLabel, leaderboardLabel, howToPlayLabel, howToIconLabel, howToSongLabel;
	private JButton backMenu;
	private JPanel helpMenuPanel;
	public HelpMenu(String name){
		userName = name;
		
		ImageIcon backBg = new ImageIcon(getClass().getResource("bgs/backBg.jpg"));
		
		playGameLabel = fillLabel("Play Game");
		settingsLabel = fillLabel("Settings");
		leaderboardLabel = fillLabel("Leaderboard");
		howToPlayLabel = fillLabel("How to Play");
		howToIconLabel = fillLabel("How to change icons");
		howToSongLabel = fillLabel("How to change song");
		
		JTabbedPane jtbExample = new JTabbedPane();
		JPanel playPanel = new JPanel();
		playPanel.add(playGameLabel);
		jtbExample.addTab("Play Game", playPanel);
		jtbExample.setSelectedIndex(0);
		JPanel settingsPanel = new JPanel();
		settingsPanel.add(settingsLabel);
		jtbExample.addTab("Settings", settingsPanel);
		JPanel leaderboardPanel = new JPanel();
		leaderboardPanel.add(leaderboardLabel);
		jtbExample.addTab("Leaderboard", leaderboardPanel);
		JPanel howToPlayPanel = new JPanel();
		howToPlayPanel.add(howToPlayLabel);
		jtbExample.addTab("How to Play", howToPlayPanel);
		JPanel howToIconPanel = new JPanel();
		howToIconPanel.add(howToIconLabel);
		jtbExample.addTab("How to change icons", howToIconPanel);
		JPanel howToSongPanel = new JPanel();
		howToSongPanel.add(howToSongLabel);
		jtbExample.addTab("How to change song", howToSongPanel);
			
		setLayout(new GridLayout(1, 2));
		add(jtbExample);
		

		
		backMenu = new JButton("Back to Main Menu", backBg);
		backMenu.setBounds(130, 310, 95, 55);
		backMenu.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	MainMenu mainMenu = new MainMenu(userName);
		    	mainMenu.setVisible(true);
		    }
		});			
		
		jtbExample.setBounds(0, 0, 390, 300);
		helpMenuPanel = new JPanel();
		helpMenuPanel.setLayout(null);
		helpMenuPanel.add(jtbExample);
		helpMenuPanel.add(backMenu);
		getContentPane().add(helpMenuPanel);


	}
	
	private JLabel fillLabel(String name){
		String labelString = "<html>";
		DatabaseManager.openConnection();
		Statement stmt = null;
		try {
			String sql = "Select * from flappybirdv2db.helptable";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int i = 0;
			while(rs.next()){
				if(rs.getString("instructions").equals(name)){
					i = 0;
					while(rs.getString("description").length() - i > 50){
						labelString = labelString + "<br>" + rs.getString("description").substring(i, i+50) + "</br>";
						i = i +50;
					}
					labelString = labelString + "<br>" + rs.getString("description").substring(i, rs.getString("description").length()) + "</br><br></br>";
				}			
			}
			DatabaseManager.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		labelString = labelString + "</html>";
		JLabel returnLabel = new JLabel(labelString);
		return returnLabel;
	}
	
	private void closeTab(){
    	this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
	}
}
